package cn.funeralobjects.dictms.service.impl;

import cn.funeralobjects.common.jpa.service.CommonCloudCrudJpaService;
import cn.funeralobjects.common.repository.CommonCloudRepository;
import cn.funeralobjects.common.service.annotataion.CommonService;
import cn.funeralobjects.common.service.exception.DataConflictException;
import cn.funeralobjects.dictms.BasicInfo;
import cn.funeralobjects.dictms.entity.EDict;
import cn.funeralobjects.dictms.model.Dict;
import cn.funeralobjects.dictms.repository.EDictRepository;
import cn.funeralobjects.dictms.service.DictService;
import cn.funeralobjects.util.Assert;
import cn.funeralobjects.util.NumberUtils;

import javax.annotation.Resource;

/**
 * @author FuneralObjects
 * Create date: 2020/6/2 11:37 PM
 */
@CommonService
public class DictServiceImpl implements DictService, CommonCloudCrudJpaService<Dict, EDict, Integer> {

    @Resource
    private EDictRepository eDictRepository;

    @Override
    public CommonCloudRepository<EDict, Integer> commonCloudRepository() {
        return eDictRepository;
    }

    @Override
    public void checkConflict(Dict dict) throws DataConflictException {
        this.mustNotConflictByName(dict.getName());
    }

    @Override
    public EDict toEntity(Dict dict) {
        if (dict == null) {
            return null;
        }
        EDict eDict = new EDict();
        eDict.setRemark(dict.getRemark())
                .setCode(dict.getCode())
                .setName(dict.getName());
        return eDict;
    }

    @Override
    public void checkConflict(Dict dict, Integer withoutId) throws DataConflictException {
        this.mustNotConflictByName(dict.getName(), withoutId);
    }

    @Override
    public void copyToEntity(Dict sourceModify, EDict targetEntity) {
        targetEntity
                .setRemark(sourceModify.getRemark())
                .setCode(sourceModify.getCode())
                .setName(sourceModify.getName());
    }

    @Override
    public boolean isValid(Integer id) {
        return NumberUtils.notZero(id);
    }

    @Override
    public void validateModify(Dict dict) {
        Assert.argAssert(dict, "dict");
    }

    @Override
    public String getName() {
        return ENTITY_NAME;
    }

    @Override
    public String getModuleName() {
        return BasicInfo.MODULE_NAME;
    }

    @Override
    public Integer getExistsDelSalt() {
        return (Integer) eDictRepository.getDefaultExistsDelSalt();
    }
}
